<template>
  <div class="add">
    <el-card>
      <div>
        <el-icon><Tickets /></el-icon>数据列表
      </div>
      <el-button type="primary" @click="router.push('/ums/addMenu')"
        >添加</el-button
      >
    </el-card>
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="菜单名称" prop="title" />
    <el-table-column label="菜单级数" prop="level">
      <template #default="{ row }">
        <span>{{
          row.level === 0 ? "一级" : row.level === 1 ? "二级" : "三级"
        }}</span>
      </template>
    </el-table-column>
    <el-table-column label="前端名称" prop="name" />
    <el-table-column label="是否显示" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.hidden"
          :active-value="0"
          :inactive-value="1"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="排序" prop="sort" />

    <el-table-column label="设置" prop="loginTime"
      ><template #default="{ row }">
        <el-button
          text
          :type="typeLevel"
          @click="searchMenuFn(row.id)"
          :disabled="searchBtnShow"
          >查看下级</el-button
        >
      </template></el-table-column
    >

    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="editFn(row.id)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <ElConfigProvider :locale="zhCn">
    <div class="page">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="queryUserList"
        @current-change="queryUserList"
      />
    </div>
  </ElConfigProvider>
</template>
<script setup lang="ts">
import { reactive, ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import { updateRoleHiddenStatusApi, pagingGetMenuListApi } from "./api/api";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ElMessage } from "element-plus";
import { useRouter, useRoute } from "vue-router";
const userList = ref<IPagingList[]>([]);
const router = useRouter();
const queryParams = reactive<IUserListParams>({
  pageNum: 1,
  pageSize: 10,
});
// 全部数量
const total = ref(0);
// 用户状态
const switchStatus = (row: IPagingList) => {
  updateRoleHiddenStatusApi(row.id!, row.hidden)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        row.hidden = row.hidden === 0 ? 1 : 0;
      }
    })
    .catch(() => {
      row.hidden = row.hidden === 0 ? 1 : 0;
    });
};
const route = useRoute();
const searchBtnShow = ref(false);
if (route.query.pid) {
  searchBtnShow.value = true;
} else {
  searchBtnShow.value = false;
}
const typeLevel = ref("primary");
const searchMenuFn = (id: number) => {
  router.push(`/ums/menu?pid=${id}`);
  pagingGetMenuListApi(id, queryParams).then((res) => {
    if (res.code === 200) {
      typeLevel.value = "info";
      searchBtnShow.value = true;
      userList.value = res.data.list;
      // queryParams.keyword = "";
      total.value = res.data.total;
      console.log("获取到菜单列表", res);
    }
  });
};
const editFn = (id: number) => {
  router.push(`/ums/addMenu?id=${id}`);
};
// 封装
const queryUserList = () => {
  pagingGetMenuListApi(0, queryParams).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      // queryParams.keyword = "";
      total.value = res.data.total;
      console.log("获取到菜单列表", res);
    }
  });
};
queryUserList();
</script>
<style lang="less" scoped>
.searchBox {
  :deep(.el-card) {
    padding: 20px;
  }
  .top {
    position: relative;
    display: flex;
    .top_left {
      .search_txt {
        padding-left: 10px;
        font-size: 14px;
      }
    }
    .top_right {
      position: absolute;
      right: 20px;
    }
  }
  .under {
    padding: 20px 40px 0;
    font-size: 14px;
    :deep(.el-input) {
      height: 32px;
      line-height: 32px;
      width: 210px;
    }
  }
}
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
}

:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
